package com.zhangzhan.gulimallproduct.app;

import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.R;
import com.zhangzhan.gulimallproduct.entity.AttrGroupEntity;
import com.zhangzhan.gulimallproduct.service.AttrGroupService;
import com.zhangzhan.gulimallproduct.service.CategoryService;
import com.zhangzhan.gulimallproduct.vos.AttrAttrGroupRelationVo;
import com.zhangzhan.gulimallproduct.vos.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author zhangzhan
 * @email sunlightcs@gmail.com
 * @date 2024-06-12 16:45:44
 */
@RestController
@RequestMapping("gulimallproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 所有列表或模糊查询
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Integer catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 修改回显信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("gulimallproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);


        Long[] catelogIdPath = categoryService.getCatelogIdPath(attrGroup.getCatelogId());

        attrGroup.setCatelogPath(catelogIdPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 查询属性组下的属性
     */
    @RequestMapping(value = "/{attrGroupId}/attr/relation", method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrVo> attrVos = attrGroupService.attrRelation(attrGroupId);

        return R.ok().put("data", attrVos);
    }


    /**
     * 删除关联属性
     */
    @RequestMapping(value = "/attr/relation/delete", method = RequestMethod.POST)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    public R attrRelationDelete(@RequestBody List<AttrAttrGroupRelationVo> vos) {
        attrGroupService.attrRelationDelete(vos);

        return R.ok();
    }

    /**
     * 获取属性分组没有关联的其他属性
     */
    @RequestMapping(value = "/{attrGroupId}/noattr/relation", method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    public R noattrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = attrGroupService.noattrRelation(params, attrGroupId);

        return R.ok().put("page", page);
    }

    /**
     * 添加属性与分组关联关系
     */
    @RequestMapping(value = "/attr/relation", method = RequestMethod.POST)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    public R attrRelationS(@RequestBody List<AttrAttrGroupRelationVo> vos) {
        attrGroupService.attrRelationS(vos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @RequestMapping(value = "/{catelogId}/withattr", method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    public R getClassifyTheAssociatedGroupingAndGroupingAttributes(@PathVariable("catelogId") Long catelogId) {
        return R.ok().put("data",attrGroupService.getClassifyTheAssociatedGroupingAndGroupingAttributes(catelogId));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallproduct:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
